'use strict';

    angular.module('app')
        .controller('sysUserController', ['$scope', '$rootScope', '$state', '$timeout',
                                             function($scope, $rootScope, $state, $timeout,toaster) {
        var title = "";
        var defaultAva = $rootScope.defaultAvatar;

         $scope.title = $rootScope.title = title;
         $scope.loading = true;
         //初始化验证
         validate();
         $scope.submitPwd= function(){
             var m = $scope.record;
             $scope.record
             if(m){
                 $scope.isDisabled = true;//提交disabled
                 $.ajax({
                     url : $scope.app.project_name+ '/user/update/passwords',
                     data: $scope.record
                 }).then(callback);
             }
             function callback(result) {
                 console.log(result);
                 if(result.httpCode ==200){//修改成功
                    if(result.data.state==1){
                        $timeout(function(){
                            $state.go('access.login');
                        },2000);
                    }else {
                        $scope.alertModal({"content":result.data.msg});
                        $scope.isDisabled = false;//提交disabled
                    }
                 }else{
                     $scope.alertModal({"content":result.msg});
                 }
                 $rootScope.$apply();
                 $scope.loading = false;
             }
         }

        //表单验证
        function validate(){
            //notEqual 规则
            $.validator.addMethod('notEqual', function(value, ele){
                return value != this.settings.rules[ele.name].notEqual;
            });
            jQuery('form').validate({
                rules: {
                    newPassword:{
                        maxlength: 16
                    },
                    confirmPassword:{
                       // required: true,
                        maxlength: 16,
                        equalTo: "#password"
                    }
                },
                messages: {
                    newPassword:{
                        //required: '请填写密码',
                        maxlength: '密码长度不可大于16位'
                    },
                    confirmPassword:{
                        //required: '请填写确认密码',
                        maxlength: '密码长度不可大于16位',
                        equalTo: '两次输入的密码不相符'
                    }
                },
                submitHandler: function() {
                    $scope.submit();
                }
            });
        }

    }]);